package com.fr.base;

import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.web.core.Attachment;
import com.fr.web.core.service.AttachmentService;
import java.awt.Image;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeUtils
{
  private static Pattern bracketPattern = Pattern.compile("\\[[^\\]]*\\]");
  private static Pattern textPattern = Pattern.compile("[0-9a-f]{2,4}", 2);
  private static Pattern blankPattern = Pattern.compile("\\s+");

  public static String javascriptEncode(String paramString)
  {
    if ((paramString == null) || (paramString.length() == 0))
      return "";
    int j = 0;
    int l = paramString.length();
    StringBuffer localStringBuffer = new StringBuffer();
    for (int k = 0; k < l; ++k)
    {
      int i = j;
      j = paramString.charAt(k);
      switch (j)
      {
      case 34:
      case 92:
        localStringBuffer.append('\\');
        localStringBuffer.append(j);
        break;
      case 47:
        if (i == 60)
          localStringBuffer.append('\\');
        localStringBuffer.append(j);
        break;
      case 8:
        localStringBuffer.append("\\b");
        break;
      case 9:
        localStringBuffer.append("\\t");
        break;
      case 10:
        localStringBuffer.append("\\n");
        break;
      case 12:
        localStringBuffer.append("\\f");
        break;
      case 13:
        localStringBuffer.append("\\r");
        break;
      default:
        if ((j < 32) || ((j >= 128) && (j < 160)) || ((j >= 8192) && (j < 8448)))
        {
          String str = "000" + Integer.toHexString(j);
          localStringBuffer.append("\\u" + str.substring(str.length() - 4));
        }
        else
        {
          localStringBuffer.append(j);
        }
      }
    }
    return localStringBuffer.toString();
  }

  public static String javascriptDecode(String paramString)
  {
    return BaseCoreUtils.encodeString(paramString, new String[][] { { "\\", "'", "\"" }, { "\\\\", "\\'", "\\\"" } });
  }

  public static String encodeURIComponent(String paramString)
  {
    // Byte code:
    //   0: aload_0
    //   1: ldc 28
    //   3: invokestatic 29	java/net/URLEncoder:encode	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   6: areturn
    //   7: astore_1
    //   8: aload_0
    //   9: iconst_2
    //   10: anewarray 19	[Ljava/lang/String;
    //   13: dup
    //   14: iconst_0
    //   15: bipush 13
    //   17: anewarray 20	java/lang/String
    //   20: dup
    //   21: iconst_0
    //   22: ldc 31
    //   24: aastore
    //   25: dup
    //   26: iconst_1
    //   27: ldc 32
    //   29: aastore
    //   30: dup
    //   31: iconst_2
    //   32: ldc 33
    //   34: aastore
    //   35: dup
    //   36: iconst_3
    //   37: ldc 34
    //   39: aastore
    //   40: dup
    //   41: iconst_4
    //   42: ldc 35
    //   44: aastore
    //   45: dup
    //   46: iconst_5
    //   47: ldc 36
    //   49: aastore
    //   50: dup
    //   51: bipush 6
    //   53: ldc 37
    //   55: aastore
    //   56: dup
    //   57: bipush 7
    //   59: ldc 38
    //   61: aastore
    //   62: dup
    //   63: bipush 8
    //   65: ldc 39
    //   67: aastore
    //   68: dup
    //   69: bipush 9
    //   71: ldc 40
    //   73: aastore
    //   74: dup
    //   75: bipush 10
    //   77: ldc 41
    //   79: aastore
    //   80: dup
    //   81: bipush 11
    //   83: ldc 42
    //   85: aastore
    //   86: dup
    //   87: bipush 12
    //   89: ldc 43
    //   91: aastore
    //   92: aastore
    //   93: dup
    //   94: iconst_1
    //   95: bipush 13
    //   97: anewarray 20	java/lang/String
    //   100: dup
    //   101: iconst_0
    //   102: ldc 44
    //   104: aastore
    //   105: dup
    //   106: iconst_1
    //   107: ldc 45
    //   109: aastore
    //   110: dup
    //   111: iconst_2
    //   112: ldc 46
    //   114: aastore
    //   115: dup
    //   116: iconst_3
    //   117: ldc 47
    //   119: aastore
    //   120: dup
    //   121: iconst_4
    //   122: ldc 48
    //   124: aastore
    //   125: dup
    //   126: iconst_5
    //   127: ldc 49
    //   129: aastore
    //   130: dup
    //   131: bipush 6
    //   133: ldc 50
    //   135: aastore
    //   136: dup
    //   137: bipush 7
    //   139: ldc 51
    //   141: aastore
    //   142: dup
    //   143: bipush 8
    //   145: ldc 52
    //   147: aastore
    //   148: dup
    //   149: bipush 9
    //   151: ldc 53
    //   153: aastore
    //   154: dup
    //   155: bipush 10
    //   157: ldc 54
    //   159: aastore
    //   160: dup
    //   161: bipush 11
    //   163: ldc 55
    //   165: aastore
    //   166: dup
    //   167: bipush 12
    //   169: ldc 56
    //   171: aastore
    //   172: aastore
    //   173: invokestatic 27	com/fr/base/core/BaseCoreUtils:encodeString	(Ljava/lang/String;[[Ljava/lang/String;)Ljava/lang/String;
    //   176: areturn
    //
    // Exception table:
    //   from	to	target	type
    //   0	6	7	java/io/UnsupportedEncodingException
  }

  public static String attributeHtmlEncode(CharSequence paramCharSequence)
  {
    if (paramCharSequence == null)
      return "";
    int i = paramCharSequence.length();
    StringBuffer localStringBuffer = new StringBuffer((int)(i * 1.5D));
    for (int j = 0; j < i; ++j)
    {
      char c = paramCharSequence.charAt(j);
      if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')))
        localStringBuffer.append(c);
      else
        localStringBuffer.append("&#").append(c).append(';');
    }
    return localStringBuffer.toString();
  }

  public static String htmlEncode(CharSequence paramCharSequence)
  {
    if (paramCharSequence == null)
      return "";
    int i = paramCharSequence.length();
    StringBuffer localStringBuffer = new StringBuffer(i);
    for (int j = 0; j < i; ++j)
    {
      char c = paramCharSequence.charAt(j);
      switch (c)
      {
      case '<':
        localStringBuffer.append("&lt;");
        break;
      case '>':
        localStringBuffer.append("&gt;");
        break;
      case '&':
        localStringBuffer.append("&amp;");
        break;
      case '"':
        localStringBuffer.append("&quot;");
        break;
      case '\r':
        if ((j + 1 < i) && (paramCharSequence.charAt(j + 1) == '\n'))
          ++j;
        localStringBuffer.append("<br>");
        break;
      case '\\':
        if ((j + 1 < i) && (paramCharSequence.charAt(j + 1) == 'n'))
        {
          ++j;
          localStringBuffer.append("<br>");
        }
        else
        {
          localStringBuffer.append(c);
        }
        break;
      case '\n':
        localStringBuffer.append("<br>");
        break;
      default:
        localStringBuffer.append(c);
      }
    }
    String str = replaceBlankToHtmlBlank(localStringBuffer.toString());
    return str;
  }

  private static String replaceBlankToHtmlBlank(String paramString)
  {
    Matcher localMatcher = blankPattern.matcher(paramString);
    int i = 0;
    int j = 0;
    StringBuffer localStringBuffer = new StringBuffer(paramString.length());
    while (true)
    {
      int k;
      do
      {
        if (!(localMatcher.find()))
          break label118;
        j = localMatcher.start();
        localStringBuffer.append(paramString.substring(i, j));
        i = localMatcher.end();
        String str = localMatcher.group();
        localStringBuffer.append(" ");
        k = str.length() - 1;
      }
      while (k <= 1);
      for (int l = k; l > 1; l -= 2)
        localStringBuffer.append("&nbsp;");
      localStringBuffer.append(" ");
    }
    if (i == 0)
      label118: return paramString;
    localStringBuffer.append(paramString.substring(i));
    return localStringBuffer.toString();
  }

  public static String htmlDecode(String paramString)
  {
    return BaseCoreUtils.encodeString(paramString, new String[][] { { " ", "&", "<", ">", "'", "\"", "\\r\\n" }, { "&nbsp;", "&amp;", "&lt;", "&gt;", "&apos;", "&quot;", "<br>" } });
  }

  public static String cjkEncode(String paramString)
  {
    if (paramString == null)
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    int j = paramString.length();
    while (i < j)
    {
      char c = paramString.charAt(i);
      if ((c >= 128) || (c == '[') || (c == ']'))
      {
        localStringBuffer.append("[");
        localStringBuffer.append(Integer.toString(c, 16));
        localStringBuffer.append("]");
      }
      else
      {
        localStringBuffer.append(c);
      }
      ++i;
    }
    return localStringBuffer.toString();
  }

  public static String cjkDecode(String paramString)
    throws Exception
  {
    if (paramString == null)
      return "";
    if (paramString.indexOf(91) == -1)
      return paramString;
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramString.length(); ++i)
    {
      char c = paramString.charAt(i);
      if (c == '[')
      {
        int j = paramString.indexOf(93, i + 1);
        if (j > i + 1)
        {
          String str = paramString.substring(i + 1, j);
          if (str.length() > 0)
            c = (char)Integer.parseInt(str, 16);
          i = j;
        }
      }
      localStringBuffer.append(c);
    }
    return localStringBuffer.toString();
  }

  public static String jsonEncode(Object paramObject)
    throws JSONException
  {
    return JSONObject.valueToString(paramObject);
  }

  public static Object jsonDecode(String paramString)
    throws JSONException
  {
    JSONArray localJSONArray = new JSONArray("[" + paramString + "]");
    return ((localJSONArray.length() == 0) ? null : localJSONArray.get(0));
  }

  public static Object object2JSONable(Object paramObject)
    throws JSONException
  {
    if (paramObject instanceof Date)
      return new JSONObject().put("date_milliseconds", new Long(((Date)paramObject).getTime()));
    if (paramObject instanceof Image)
      return AttachmentService.addAttachment((Image)paramObject).toConfig();
    return paramObject;
  }

  public static String evadeBackslash(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = paramString.length();
    for (int j = 0; j < i; ++j)
      if (paramString.charAt(j) == '\\')
        localStringBuffer.append("\\\\");
      else
        localStringBuffer.append(paramString.charAt(j));
    return localStringBuffer.toString();
  }

  public static String escape(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.ensureCapacity(paramString.length() * 6);
    for (int i = 0; i < paramString.length(); ++i)
    {
      int j = paramString.charAt(i);
      if ((Character.isDigit(j)) || (Character.isLowerCase(j)) || (Character.isUpperCase(j)))
      {
        localStringBuffer.append(j);
      }
      else if (j < 256)
      {
        localStringBuffer.append("%");
        if (j < 16)
          localStringBuffer.append("0");
        localStringBuffer.append(Integer.toString(j, 16));
      }
      else
      {
        localStringBuffer.append("%u");
        localStringBuffer.append(Integer.toString(j, 16));
      }
    }
    return localStringBuffer.toString();
  }

  public static String unescape(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.ensureCapacity(paramString.length());
    int i = 0;
    int j = 0;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            if (i >= paramString.length())
              break label158;
            j = paramString.indexOf("%", i);
            if (j != i)
              break label119;
            if (paramString.charAt(j + 1) != 'u')
              break;
            c = (char)Integer.parseInt(paramString.substring(j + 2, j + 6), 16);
            localStringBuffer.append(c);
            i = j + 6;
          }
          char c = (char)Integer.parseInt(paramString.substring(j + 1, j + 3), 16);
          localStringBuffer.append(c);
          i = j + 3;
        }
        label119: if (j != -1)
          break;
        localStringBuffer.append(paramString.substring(i));
        i = paramString.length();
      }
      localStringBuffer.append(paramString.substring(i, j));
      i = j;
    }
    label158: return localStringBuffer.toString();
  }

  public static boolean isCJKEncoded(String paramString)
  {
    int i = 0;
    if (paramString == null)
      return i;
    Matcher localMatcher = bracketPattern.matcher(paramString);
    while (true)
    {
      if (!(localMatcher.find()))
        break label68;
      i = 1;
      int j = localMatcher.start();
      int k = localMatcher.end();
      String str = paramString.substring(j + 1, k - 1);
      if (!(textPattern.matcher(str).matches()))
        break;
    }
    return false;
    label68: return i;
  }

  public static String carlDecode(String paramString)
  {
    if (paramString == null)
      return null;
    paramString = paramString.replaceAll("%fr22", "\"");
    paramString = paramString.replaceAll("%fr27", "'");
    return paramString;
  }
}